import axios from 'axios'
import { Loading } from 'element-ui'
import {getTokenLocalStorage} from "@/utils/utilLocalStorage";
const service = axios.create({
  // baseURL: 'https://api-hmzs.itheima.net/v1',
  baseURL: 'http://localhost:8081',
  timeout: 5000 // request timeout
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    // config.headers.Authorization = 'Bearer ' + getTokenLocalStorage()
    config.headers.Authorization = localStorage.getItem('token')
    const options = {
      lock: true,
      text: '加载中...',
      background: '(122, 122, 122, 0.8)'
    }
    Loading.service(options)
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    const options = {
      lock: true,
      text: '加载中...',
      background: '(122, 122, 122, 0.8)'
    }
    Loading.service(options).close()
    return response.data
  },
  error => {
    const options = {
      lock: true,
      text: '加载中...',
      background: '(122, 122, 122, 0.8)'
    }
    Loading.service(options).close()
    // if (error.response.status === 401) {
    //   router.push('/login')
    //   Message.error('登录过期')
    // }
    return Promise.reject(error)
  }
)

export default service
